package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.DisinfecResultDetailItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.QuarantineResultDetailItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.QuarRsltDtailItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class DisinfectionStatusDetailThread extends BasicThread {

	public interface DisinfectionStatusDetailThreadCallback {
		public void OnDisinfectionStatusDetailCompleted(boolean isNetworkConntected
				, DisinfecResultDetailItem quarantineItem
				, String message);
		public void OnDisinfectionStatusDetailError(ErrorItem errorItem);
	}

	private String url;
	private DisinfectionStatusDetailThreadCallback callback;
	private ArrayList<NameValuePair> post;
	private boolean value;
	
	/* 파라메터*/
	private String recNo, offCd;

	public DisinfectionStatusDetailThread(Context context
			, DisinfectionStatusDetailThreadCallback callback
			, String recNo, String offCd, boolean value, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.recNo = recNo;
		this.offCd = offCd;
		this.value = value;
		this.appStatus = (AppStatus) context.getApplicationContext();
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String param = "";
		
		if(value){
			//recNo로 검색 -검색버튼 눌렀을때
			map.put(Const.RECNO, recNo);
			map.put(Const.OFFCD, offCd);
		}else{
			//resNo로 검색 -리스트에서 눌럿을때
			map.put(Const.DSNFRESNO, recNo);
			map.put(Const.DSNFRESOFFCD, offCd);
		}
		param = parameterReturn(Const.REQ034, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnDisinfectionStatusDetailCompleted(false, null, "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnDisinfectionStatusDetailError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				callback.OnDisinfectionStatusDetailError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arraydisinfection = json.getJSONArray(Const.DATA);
				JSONObject disinfection = arraydisinfection.getJSONObject(0);
				
				DisinfecResultDetailItem disinfectionItem = new DisinfecResultDetailItem();
				
				ArrayList<QuarRsltDtailItem> arrayItems = new ArrayList<QuarRsltDtailItem>();
				JSONArray items	 	= json.getJSONArray(DisinfecResultDetailItem.ITEMS);
				for(int i = 0; i < items.length(); i++) {
					JSONObject itemObject = items.getJSONObject(i);
					QuarRsltDtailItem quarRsltDtailItem = new QuarRsltDtailItem();
					//quarRsltDtailItem.setOffCd(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.OFFCD));
					//quarRsltDtailItem.setRecNo(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.RECNO));
					//quarRsltDtailItem.setRecSeq(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.RECSEQ));
					//quarRsltDtailItem.setDsnfTyName(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.DSNFTYNAME));
					//quarRsltDtailItem.setDsnfResOffCd(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.DSNFRESOFFCD));
					//quarRsltDtailItem.setVenNm(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.VENNM));
					//quarRsltDtailItem.setRecSeq(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.RECSEQ));
					//quarRsltDtailItem.setPrdlstCode(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.PRDLSTCODE));
					//quarRsltDtailItem.setPrdlstName(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.PRDLSTNAME));
					
					quarRsltDtailItem.setResOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.RESOFFCD)));
					quarRsltDtailItem.setResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.RESNO)));
					quarRsltDtailItem.setFullDsnfResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.FULLDSNFRESNO)));
					quarRsltDtailItem.setDsnfTyNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.DSNFTYNM)));
					quarRsltDtailItem.setFullRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.FULLRECNO)));
					quarRsltDtailItem.setNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.NATNM)));
					quarRsltDtailItem.setFullPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.FULLPRDLSTNM)));
					
					quarRsltDtailItem.setInsQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.INSQTY)));
					quarRsltDtailItem.setAntQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.ANTQTY)));
					quarRsltDtailItem.setDocScrtySe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.DOCSCRTYSE)));
					//quarRsltDtailItem.setQunCd(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.QUNCD));
					//quarRsltDtailItem.setQunNm(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.QUNNM));
					//quarRsltDtailItem.setDsnfCmmndNo(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.DSNFCMMNDNO));
					
					arrayItems.add(quarRsltDtailItem);
				}
				disinfectionItem.setItems(arrayItems);
				
				disinfectionItem.setItemsResult(json.getString(DisinfecResultDetailItem.ITEMSRESULT));
				//disinfectionItem.setPlanSn(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.PLANSN));
				//disinfectionItem.setEntrpsUnitySn(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ENTRPSUNITYSN));
				//disinfectionItem.setCprNm(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.CPRNM));
				disinfectionItem.setDsnfResOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.DSNFRESOFFCD)));
				disinfectionItem.setDsnfResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.DSNFRESNO)));
				disinfectionItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OFFCD)));
				disinfectionItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.RECNO)));
				disinfectionItem.setResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.RESNO)));
				disinfectionItem.setVenCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.VENCD)));
				disinfectionItem.setVenNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.VENNM)));
				disinfectionItem.setAntDatefr(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ANTDATEFR)));
				disinfectionItem.setAntDateto(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ANTDATETO)));
				disinfectionItem.setMedNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDNM)));
				disinfectionItem.setPasDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.PASDE)));
				disinfectionItem.setEquKindNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.EQUKINDNM)));
				disinfectionItem.setAntMetNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ANTMETNM)));
				disinfectionItem.setResultProgrsNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.RESULTPROGRSNM)));
				
				disinfectionItem.setEmpNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.EMPNM)));
				disinfectionItem.setTelNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.TELNO)));
				disinfectionItem.setDsnfInspctPlace(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.DSNFINSPCTPLACE)));
				//disinfectionItem.setMdctDe(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MDCTDE));
				//disinfectionItem.setMdctHh(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MDCTHH));
				//disinfectionItem.setMdctMi(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MDCTMI));
				//disinfectionItem.setOpnDe(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPNDE));
				//disinfectionItem.setOpnHh(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPNHH));
				//disinfectionItem.setOpnMi(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPNMI));
				disinfectionItem.setAntMin(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ANTMIN)));
				//disinfectionItem.setAntMetCd(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ANTMETCD));
				//disinfectionItem.setMedCd(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDCD));
				//disinfectionItem.setMdctStackCnt(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MDCTSTACKCNT));
				//disinfectionItem.setEquKind(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.EQUKIND));
				disinfectionItem.setMedQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDQTY)));
				disinfectionItem.setAntMet(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ANTMET)));
				disinfectionItem.setFumQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.FUMQTY)));
				disinfectionItem.setRotDiv(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ROTDIV)));
				disinfectionItem.setRooTemp(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ROOTEMP)));
				//disinfectionItem.setMedNamCd1(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDNAMCD1));
				//disinfectionItem.setMedNamCd2(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDNAMCD2));
				disinfectionItem.setMedNamNm1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDNAMNM1)));
				disinfectionItem.setMedNamNm2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDNAMNM2)));
				//disinfectionItem.setOpeNamCd1(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPENAMCD1));
				//disinfectionItem.setOpeNamCd2(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPENAMCD2));
				disinfectionItem.setOpeNamNm1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPENAMNM1)));
				disinfectionItem.setOpeNamNm2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPENAMNM2)));
				//사진
				disinfectionItem.setImgResult1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, QuarRsltDtailItem.IMGRESULT1)));
				disinfectionItem.setImgResult2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, QuarRsltDtailItem.IMGRESULT2)));
				disinfectionItem.setFileId(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, QuarRsltDtailItem.FILEID)));
				
				
				callback.OnDisinfectionStatusDetailCompleted(true, disinfectionItem, mResult);
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
	}
}
